package com.renjibo.childred.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.renjibo.childred.R;
import com.renjibo.childred.modle.FindGoodsBean;
import com.renjibo.childred.utils.PostHead;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class ResultFragment extends Fragment implements View.OnClickListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private Button result_button;
    private List<FindGoodsBean.ProductlistBean> productArray=new ArrayList<FindGoodsBean.ProductlistBean>();

    public ResultFragment() {

    }


    public static ResultFragment newInstance(String param1, String param2) {
        ResultFragment fragment = new ResultFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_result, container, false);
        initView(view);
        getFindGoods(mParam1,1,20);
        return view;
    }
    //请求数据
    public void getFindGoods(String keyword,int page,int pageNum){
        RequestParams params = new RequestParams(PostHead.head+"/search?keyword="+keyword+"&&page="+page+"&&pageNum="+pageNum);
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                FindGoodsBean findGoodsBean = PostHead.gson.fromJson(result, FindGoodsBean.class);
                List<FindGoodsBean.ProductlistBean> productlist = findGoodsBean.getProductlist();
                Log.d("zzz","pr:"+productlist.toString());
                productArray.addAll(productlist);


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    public void initView(View view){
        result_button = (Button) view.findViewById(R.id.result_button);
        result_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //回退
            case R.id.result_button:
                getActivity().getSupportFragmentManager().popBackStack("home",1);
                break;
        }
    }
}
